<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 第三方服务
 *
 * @author      Eden
 * @datetime    2016/12/7 17:56
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class App extends ADM_Controller
{

    // 最大上传尺寸 200KB
    const MAX_UPLOAD_SIZE = 300 * 1024;

    private $keys = array('general', 'download', 'description', 'privicy', 'adv');

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/system/app.html');
    }

    /**
     * 获取配置
     */
    public function getConfig()
    {
        $send['state'] = false;
        do{
            $type = $this->input->get('type');
            if (!empty($type) && !in_array($type, $this->keys)) {
                $send['error'] = '无此配置';
                break;
            }

            $this->load->model('admin/system/Setting_Model', 'setting');
            $search = !empty($type) ? array($type) : $this->keys;
            $result = $this->setting->get('mobile', $search);

            if (!empty($result['general'])) {
                $result['general']['slideNum'] = intval($result['general']['slideNum']);
                $result['general']['slideTimer'] = intval($result['general']['slideTimer']);
                $result['show']['logoUrl'] = thumb($result['general']['logo'], 190, 44);
            }

            if (!empty($result['adv']['imageUrl'])) {
                $result['show']['advUrl'] = thumb($result['adv']['imageUrl'], 640, 1136);
            }

            if ($result) {
                $send['state'] = true;
                $send['data'] = $result;
                break;
            }

            $send['error'] = '暂无数据';
        } while(false);
        $this->output->send($send);
    }

    public function saveConfig()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $key = $this->input->get('key');
            $params = $this->input->post();
            if (empty($key) || empty($params) || !in_array($key, $this->keys)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Setting_Model', 'setting');
            if ($res = $this->setting->save($key, $params, 'mobile')) {
                $send['state'] = true;
            } else {
                $send['error'] = '保存失败';
            }

        } while(false);
        $this->output->send($send);
    }

    public function upload()
    {
        $send['state'] = false;
        do {
            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $type = $this->input->get('type');;

            if (empty($type) || !in_array($type, array('logo', 'adv'))) {
                $send['error'] = '请求错误';
                break;
            }

            if (empty($_FILES['file'])) {
                $send['error'] = '图片不合法';
                break;
            }

            if ($type == 'logo') {
                if (!empty($_FILES['file']['size']) && $_FILES['file']['size'] > self::MAX_UPLOAD_SIZE) {
                    $send['error'] = '图片尺寸超过 300KB';
                    break;
                }
            }

            $res = local($_FILES['file']);
            if (empty($res)) {
                $send['error'] = '上传失败';
            } else {
                $send['state'] = true;
                if ($type == 'logo') {
                    $send['data'] = array_merge($res, array('fullPath' => thumb($res['key'], 190, 44)));
                } else {
                    $send['data'] = array_merge($res, array('fullPath' => thumb($res['key'], 640, 1136)));
                }
            }
        } while(false);
        $this->output->send($send);
    }
}
